#!/usr/bin/env python2.7

# retriever.py

import os, sys
#import re
#from datetime import datetime, timedelta
#from dateutil.parser import parse
import numpy as np

from metlib.kits import isinteger, Setter
from metlib.lidar import *
#from mpl_toolkits.basemap import Basemap
#from matplotlib import mlab
#from netCDF4 import Dataset
from PySide.QtCore import *
from PySide.QtGui import *
from ui_retriever import *

class Retriever(QWidget, Ui_RetrieveWidget, Setter):
    def __init__(self, parent=None):
        super(Retriever, self).__init__(parent)
        self.setupUi(self)
        self.parent = parent

        self.proflist = ['midlatitude_summer',
                 'midlatitude_winter',
                  'subarctic_summer',
                   'subarctic_winter',
                    'tropical',
                     'us1976']
        self.profBox.addItems(self.proflist)
        self.atmos_prof = 'us1976'
        self.profd = dict()
        for profname in self.proflist:
            self.profd[profname] = eval('prof_' + profname)

        self.lidard_bak = Null
        self.lidard = Null
        self.fillBox.valueChanged.connect(self.fill_lower)
        self.profBox.currentIndexChanged.connect(self.calc_prof)

    def load_lidard(self, lidard):
        self.chnBox.setMaximum(lidard['data'].shape[1]-1)
        self.lidard_bak = lidard
        self.fill_lower()
        self.calc_prof()

    def fill_lower(self):
        if self.lidard_bak:
#            print "fill_lower"
            self.lidard = self.lidard_bak.copy()
            fill_lower_part(self.lidard, self.fill_n, 1)

    def calc_prof(self):
        if self.lidard:
#            print "calc_prof"
            p, t = interp_p_t_profile(self.profd[self.atmos_prof], self.lidard['distance'])
            self.betam = calc_betam(p, t)

    def retrieve(self):
        return fernald(self.lidard, self.lc*self.lcx, self.sa, self.betam, 
                fill_index=self.fill_n, C_contains_E=self.C_contains_E, 
                apply_on_data=False)

    @property
    def sa(self):
        return self.saBox.value()

    @sa.setter
    def sa(self, value):
        self.saBox.setValue(value)

    @property
    def lc(self):
        return self.lcBox.value()

    @lc.setter
    def lc(self, value):
        self.lcBox.setValue(value)

    @property
    def lcx(self):
        return float(self.lcxEdit.text())

    @lcx.setter
    def lcx(self, value):
        self.lcxEdit.setText(str(value))
    
    @property
    def chn(self):
        return self.chnBox.value()

    @chn.setter
    def chn(self, value):
        self.chnBox.setValue(value)

    @property
    def fill_n(self):
        return self.fillBox.value()

    @fill_n.setter
    def fill_n(self, value):
        self.fillBox.setValue(value)

    @property
    def atmos_prof(self):
        return self.profBox.currentText()

    @atmos_prof.setter
    def atmos_prof(self, value):
        self.profBox.setCurrentIndex(self.profBox.findText(value))

    @property
    def C_contains_E(self):
        return self.CE_checkBox.isChecked()

    @C_contains_E.setter
    def C_contains_E(self, value):
        if value:
            self.CE_checkBox.setCheckState(Qt.Checked)
        else:
            self.CE_checkBox.setCheckState(Qt.Unchecked)

if __name__ == '__main__':
    pass
